#!/bin/bash

###################
# 常量定义
###################
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m'

readonly LOG_FILE="/var/log/games_deployment.log"
readonly SCRIPTS_DIR="/tmp/deployment_scripts"

###################
# 工具函数
###################
log_info() { 
    echo -e "${GREEN}[INFO] $1${NC}"
    echo "[INFO] $1" >> "$LOG_FILE"
}

log_warn() { 
    echo -e "${YELLOW}[WARN] $1${NC}"
    echo "[WARN] $1" >> "$LOG_FILE"
}

log_error() { 
    echo -e "${RED}[ERROR] $1${NC}"
    echo "[ERROR] $1" >> "$LOG_FILE"
}

# 错误处理
handle_error() {
    local exit_code=$?
    local command=$1
    
    log_error "命令执行失败: $command"
    log_error "错误代码: $exit_code"
    
    cleanup_and_exit $exit_code
}

# 清理并退出
cleanup_and_exit() {
    local exit_code=${1:-1}
    if [ -d "$SCRIPTS_DIR" ]; then
        log_warn "清理临时目录..."
        rm -rf "$SCRIPTS_DIR"
    fi
    exit $exit_code
}

###################
# 系统检查函数
###################
check_system_requirements() {
    log_info "检查系统要求..."
    
    # 检查是否为root用户
    if [[ $EUID -ne 0 ]]; then
        log_error "此脚本必须以root用户运行"
        exit 1
    fi
    
    # 检查是否为RHEL系统
    if ! grep -qi "red hat\|centos\|rocky\|almalinux" /etc/redhat-release; then
        log_error "此脚本仅支持Red Hat系列操作系统"
        exit 1
    fi
    
    # 检查内存
    local total_mem=$(free -m | awk '/^Mem:/{print $2}')
    if [ $total_mem -lt 2048 ]; then
        log_error "系统内存不足，需要至少2GB内存"
        exit 1
    fi
}

###################
# 下载脚本函数
###################
download_deployment_scripts() {
    log_info "下载部署脚本..."
    
    mkdir -p "$SCRIPTS_DIR"
    cd "$SCRIPTS_DIR"
    
    # wget选项
    local wget_opts="--no-check-certificate --tries=3 --timeout=15"
    
    # 下载game_platform部署脚本
    if ! wget $wget_opts -O game_platform_deploy.sh https://gitee.com/ruan-yuguang/game-platform/raw/master/deploy.sh; then
        log_error "下载game_platform部署脚本失败"
        cleanup_and_exit 1
    fi
    
    # 下载games部署脚本
    if ! wget $wget_opts -O games_deploy.sh https://gitee.com/ruan-yuguang/linux_game/raw/master/deploy.sh; then
        log_error "下载games部署脚本失败"
        cleanup_and_exit 1
    fi
    
    # 添加执行权限
    chmod +x *.sh
}

###################
# 部署函数
###################
deploy_game_platform() {
    log_info "开始部署game_platform项目..."
    
    cd "$SCRIPTS_DIR"
    if ! ./game_platform_deploy.sh; then
        log_error "game_platform部署失败"
        return 1
    fi
    
    log_info "game_platform部署完成"
    return 0
}

deploy_games() {
    log_info "开始部署games项目..."
    
    cd "$SCRIPTS_DIR"
    if ! ./games_deploy.sh; then
        log_error "games部署失败"
        return 1
    fi
    
    log_info "games部署完成"
    return 0
}

###################
# 主函数
###################
main() {
    echo -e "${GREEN}=== 游戏平台综合部署脚本 ===${NC}"
    echo "作者: ruan-yuguang"
    
    # 初始化日志文件
    touch "$LOG_FILE"
    chmod 644 "$LOG_FILE"
    
    # 系统检查
    check_system_requirements
    
    # 下载部署脚本
    download_deployment_scripts
    
    # 部署确认
    echo -e "\n${YELLOW}此脚本将依次部署以下项目：${NC}"
    echo "1. game_platform - Web游戏平台"
    echo "2. games - 数独和数织游戏"
    echo -e "\n${YELLOW}注意：${NC}"
    echo "- 建议在全新的服务器上进行部署"
    echo "- 部署过程中可能需要输入一些配置信息"
    echo "- 整个部署过程可能需要30分钟或更长时间"
    echo "- 部署日志将保存在: $LOG_FILE"
    
    read -p "是否继续部署？[y/N] " confirm
    if [[ ! $confirm =~ ^[Yy]$ ]]; then
        log_info "用户取消部署"
        cleanup_and_exit 0
    fi
    
    # 1. 部署game_platform
    if ! deploy_game_platform; then
        log_error "game_platform部署失败，终止部署"
        cleanup_and_exit 1
    fi
    
    # 等待一段时间确保服务稳定
    log_info "等待服务稳定..."
    sleep 10
    
    # 2. 部署games
    if ! deploy_games; then
        log_error "games部署失败"
        cleanup_and_exit 1
    fi
    
    # 部署完成
    echo -e "\n${GREEN}=== 部署完成 ===${NC}"
    echo "请查看各个项目的部署日志获取访问地址和其他信息"
    echo "综合部署日志位置: $LOG_FILE"
    
    # 清理临时文件
    cleanup_and_exit 0
}

# 设置错误处理
set -e
trap 'handle_error "$BASH_COMMAND"' ERR

# 执行主函数
main "$@" 